/**
 * 同步任务 - 编辑页面
 * @author yuan_rz
 * @date 2019/03/12 18:03
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        isJson = function (rule, value, callback) {
            if(value == null || value == ''){
                callback();
            }else{
                try {
                    var obj = JSON.parse(value);
                    if(typeof obj == 'object' && obj ){
                        callback();
                    }else{
                        callback(new Error('内容必须符合JSON格式'));
                    }
                } catch(e) {
                    callback(new Error('内容必须符合JSON格式'));
                }
            }
        },
        vueData = {
            isAdd: false,
            form: '',
            rules: {
                LOAD_NAME: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                LOAD_FILE: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                LOAD_PARAMS:[
                    {validator: isJson, trigger: 'change'}
                ]
            },
            selectOptions:{
                fileList : []
            }
        },
        callObj
    ;

    var loadEdit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'load_edit'
            }, win);
        },
        initData: function(){
            app.ajax({
                xurl: 'sys/load/getFileList',
                xdata: {},
                cb: function (result) {
                    if (result.ok) {
                        vueData.selectOptions.fileList = result.data.data;
                    }
                }
            });
        },
        editData: function () {
            app.ajax({
                xurl: 'sys/load/editData',
                xdata: vueData.form,
                loading: true,
                okmsg: self._moduleName + '信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('load').reloadTable();
                        self._closeTpl();
                    }
                }
            });
        },
        addData: function () {
            app.ajax({
                xurl: 'sys/load/addData',
                xdata: vueData.form,
                loading: true,
                okmsg: self._moduleName + '信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('load').reloadTable();
                        self._closeTpl();
                    }
                }
            });
        },
        _call: function (tagData) {
            if (tagData.is_add) {
                vueData.isAdd = true;
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        LOAD_TRANDATE: '2000-01-01 00:00:00'
                    },
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                callObj = tagData.obj;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
            }

        },
        _ready: function () {
            vueData.form = app.fields_(['sys_load']);
            var vue = new Vue({
                el: '#load_edit',
                data: vueData,
                methods: {
                    cancel: function () {
                        self._closeTpl();
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.addData();
                                } else {
                                    self.editData();
                                }
                            }
                        });
                    }
                },
                created: function () {
                    self._setEleIsReady('load_edit');
                }
            });
            self.initData();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new loadEdit();

});